package daiwei.javaWeb.exam.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import daiwei.javaWeb.exam.meta.BoughtProduct;
import daiwei.javaWeb.exam.meta.Product;
import daiwei.javaWeb.exam.meta.User;

@Repository("Mapper")
public interface Mapper {
	
	@Results({
		@Result(property="id",column="id"),
		@Result(property="username", column="userName"),
		@Result(property="usertype", column="userType")
	})
	@Select("select id, userName, userType from person where userName = #{0} and password =#{1}")
	public User getUser(String userName, String password) throws Exception ;
	
	@Results({
		@Result(property="id",column="id"),
		@Result(property="price",column="price"),
		@Result(property="title",column="title"),
		@Result(property="image",column="icon"),
		@Result(property="summary",column="abstract"),
		@Result(property="detail",column="text")
	})
	@Select("select id, price,title,icon,abstract,text from content")
	public List<Product> getContents() throws Exception;
	
	@Results({
		@Result(property="id",column="id"),
		@Result(property="price",column="price"),
		@Result(property="title",column="title"),
		@Result(property="image",column="icon"),
		@Result(property="summary",column="abstract"),
		@Result(property="detail",column="text")
	})
	@Select("select id, price,title,icon,abstract,text from content where id=#{0}")
	public Product getContent(int id) throws Exception;
	
	@Results({
		@Result(property="id",column="id"),
		@Result(property="title",column="title"),
		@Result(property="image",column="icon"),
		@Result(property="buyPrice",column="price"),
		@Result(property="detail",column="text"),
		@Result(property="summary",column="abstract"),
		@Result(property="buyTime",column="time")
	})
	@Select("select p.id,p.title,p.icon,p.abstract,t.price,t.time,p.text from content p, person u, trx t where p.id = t.contentId and u.id = t.personId and u.id = #{0}")
	public List<BoughtProduct> getBoughtProducts(int userId) throws Exception;

	@Results({
		@Result(property="id",column="id"),
		@Result(property="title",column="title"),
		@Result(property="image",column="icon"),
		@Result(property="price",column="price"),
		@Result(property="buyPrice",column="txprice"),
		@Result(property="detail",column="text"),
		@Result(property="summary",column="abstract"),
		@Result(property="buyTime",column="time")
	})
	@Select("select p.id,p.title,p.icon,p.abstract,p.price,t.price txprice,t.time,p.text from content p, person u, trx t where p.id = t.contentId and u.id = t.personId and u.id = #{0} and p.id=#{1}")
	public BoughtProduct getBoughtProduct(int userId, int contentId) throws Exception;

	
	@Insert("insert into content(price,title,icon,abstract,text) values(#{price}, #{title}, #{image},#{summary},#{detail})")
	public void addProduct(Product p) throws Exception;

	@Select("SELECT LAST_INSERT_ID();")
	public int getContentId() throws Exception;
	
	@Insert("insert into trx(contentId,personId,price,time) values(#{0},#{1},#{2},#{3})")
	public void buyProduct(int contentId, int userId, long price, long time) throws Exception;

	@Results({
		@Result(property="id",column="contentId"),
		@Result(property="price",column="price")
	})
	@Select ("select contentId,price from trx where contentId=#{0}")
	public Product productisSell(int productId) throws Exception;

	@Update("update content set price =#{price},title=#{title},icon=#{image},abstract=#{summary},text=#{detail} where id=#{id}")
	public void editContent(Product p) throws Exception;

	@Delete("delete from content where id=#{id}")
	public void deleteContent(int id) throws Exception;

}
